﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.BLL
{
    public static class WebcarDataHelper
    {
        static WebcarDataHelper()
        {
            m_car_status_code = new List<Tuple<string, string>>() { 
                new Tuple<string, string>("T","正常"),
                new Tuple<string, string>("F","报废"),
                new Tuple<string, string>("D","删除")
            };
            m_driver_status_code = new List<Tuple<string, string>>() { 
                new Tuple<string, string>("T","正常"),
                new Tuple<string, string>("F","删除")
            };
            m_car_use_code = new List<Tuple<string, string>>() { 
                new Tuple<string, string>("01","生产"),
                new Tuple<string, string>("02","公务"),
                new Tuple<string, string>("03","集体"),
                new Tuple<string, string>("04","其他")

                //new Tuple<string, string>("03","三产"),

                //new Tuple<string, string>("04","农电"),
                //new Tuple<string, string>("05","自备车"),
                //new Tuple<string, string>("06","私家车")

            };

            m_yesno = new List<Tuple<string, string>>() { 
                new Tuple<string, string>("T","是"),
                new Tuple<string, string>("F","否")
            };
            m_yesno2 = new List<Tuple<string, string>>() { 
                new Tuple<string, string>("1","是"),
                new Tuple<string, string>("0","否")
            };

            m_base_kind = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("B","车辆修理类别"),
                new Tuple<string,string>("C","资产所属单位"),
                new Tuple<string,string>("D","事故类型"),
                new Tuple<string,string>("E","车型"),
                new Tuple<string,string>("G","事故赔款费类别"),
                new Tuple<string,string>("H","下级单位分类"),
                new Tuple<string,string>("I","文件分类"),
                new Tuple<string,string>("K","第二车型"),
                new Tuple<string,string>("M","法律法规类型")

            };
            m_job_kind = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("1","专职"),
                new Tuple<string,string>("2","兼职"),
                new Tuple<string,string>("3","聘用"),
                new Tuple<string,string>("4","自驾"),
                new Tuple<string,string>("5","专转"),
                new Tuple<string,string>("6","摩驾")
            };
            m_class_level = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("01","初级"),
                new Tuple<string,string>("02","中级"),
                new Tuple<string,string>("03","高级"),
                new Tuple<string,string>("04","技师"),
                new Tuple<string,string>("05","高级技师")

            };

            m_sex_kind = new List<Tuple<string, string>>() {
                new Tuple<string,string>("1","男"),
                new Tuple<string,string>("0","女")

            };
            m_education  = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("0","大学以上"),
                new Tuple<string,string>("1","本科"),
                new Tuple<string,string>("2","专科"),
                new Tuple<string,string>("3","中专"),
                new Tuple<string,string>("4","高中"),
                new Tuple<string,string>("5","初中"),
                new Tuple<string,string>("6","小学")
            };
            m_sec_type = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("01","一般"),
                new Tuple<string,string>("02","轻微"),
                new Tuple<string,string>("03","重大"),
                new Tuple<string,string>("04","特大")
            };
            m_sec_duty = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("01","全责"),
                new Tuple<string,string>("02","主责"),
                new Tuple<string,string>("03","同责"),
                new Tuple<string,string>("04","次责"),
                new Tuple<string,string>("05","无责"),
                new Tuple<string,string>("06","待定")

            };
            m_report_kind = new List<Tuple<string, string>>() { 
                new Tuple<string,string>("P","车管工作汇报"),
                new Tuple<string,string>("R","安全学习内容")

            };
            
        }

        static List<Tuple<string, string>> m_car_status_code = null;
        static List<Tuple<string, string>> m_car_use_code = null;
        static List<Tuple<string, string>> m_yesno = null;
        static List<Tuple<string, string>> m_yesno2 = null;
        static List<Tuple<string, string>> m_base_kind = null;
        static List<Tuple<string, string>> m_job_kind = null;
        static List<Tuple<string, string>> m_sex_kind = null;
        static List<Tuple<string, string>> m_class_level = null;
        static List<Tuple<string, string>> m_education = null;
        static List<Tuple<string, string>> m_sec_type = null;
        static List<Tuple<string, string>> m_sec_duty = null;
        static List<Tuple<string, string>> m_report_kind = null;
        static List<Tuple<string, string>> m_driver_status_code = null;
        public static List<Tuple<string, string>> ReportKind
        {
            get
            {
                return m_report_kind;
            }
        }
        public static bool CheckColumns(string table_name, string colname, string type)
        {
            string sql = "select count(1) from syscolumns where id = object_id('{0}') and name='{1}'".FormatWith(table_name, colname);
            var ret = WebcarDataProvider.DB.ExecuteScarlar(sql).ToInt();
            
            if (ret == 0)
            {
                string create_sql = "alter table " + table_name + " add " + colname + " " + type;
                WebcarDataProvider.DB.ObjectContext.ExecuteStoreCommand(create_sql);
                return false;
            }
            return true;
        }
        public static List<Tuple<string, string>> FileKind
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == "I"
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                //ret.Insert(0, new Tuple<string, string>("", "--请选择--"));
                return ret;
            }
        }
        public static List<Tuple<string, string>> LawKind
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == "M"
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                //ret.Insert(0, new Tuple<string, string>("", "--请选择--"));
                return ret;
            }
        }
        public static List<Tuple<string, string>> RepairKind
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == AppConstant.REPAIR_KIND_KEY
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                //ret.Insert(0, new Tuple<string, string>("", "--请选择--"));
                return ret;
            }
        }

        public static List<Tuple<string, string>> SecDuty
        {
            get
            {
                return m_sec_duty;
            }
        }
        public static List<Tuple<string, string>> SecType
        {
            get
            {
                return m_sec_type;
            }
        }

        public static List<Tuple<string, string>> Education
        {
            get
            {
                return m_education;
            }
        }

        public static List<Tuple<string, string>> ClassLevel
        {
            get
            {
                return m_class_level;
            }
        }

        public static List<Tuple<string, string>> SexKind
        {
            get
            {
                return m_sex_kind;
            }
        }
        public static List<Tuple<string, string>> JobKind
        {
            get
            {
                return m_job_kind;
            }
        }

        public static List<Tuple<string, string>> BaseKind
        {
            get
            {
                return m_base_kind;
            }
        }
        public static List<Tuple<string, string>> YesNoCode
        {
            get
            {
                return m_yesno;
            }
        }
        public static List<Tuple<string, string>> YesNoCode2
        {
            get
            {
                return m_yesno2;
            }
        }
        public static List<Tuple<string, string>> DriverStatusCode
        {
            get
            {
                return m_driver_status_code;
            }
        }
        public static List<Tuple<string, string>> CarStatusCode
        {
            get
            {
                return m_car_status_code;
            }
        }
        public static List<Tuple<string, string>> CarUseCode
        {
            get
            {
                return m_car_use_code;
            }
        }
        public static List<Tuple<string, string>> SecKind
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == AppConstant.SEC_KIND_KEY
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                //ret.Insert(0, new Tuple<string, string>("", "--请选择--"));
                return ret;
            }
        }

        public static List<Tuple<string, string>> DeptKindCode
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == AppConstant.DEPT_KIND_KEY
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                ret.Insert(0, new Tuple<string, string>("", "--请选择--"));
                return ret;
            }
        }

        public static List<Tuple<string, string>> CarSortCode
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == AppConstant.CAR_SORT_KEY
                           select p;
                var ret = list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
                ret.Insert(0,new Tuple<string, string>("","--请选择--"));
                return ret;
            }
        }

        public static List<Tuple<string, string>> DeptKindList
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == AppConstant.DEPT_KIND_KEY
                           select p;
                return list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
            }
        }

        public static List<Tuple<string, string>> AssertKind
        {
            get
            {
                var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CODE
                           where p.BASE_KIND == "C"
                           select p;
                return list.AsEnumerable().Select(p => new Tuple<string, string>(p.BASE_CODE, p.BASE_NAME)).ToList();
            }
        }

        public static SelectList BaseKindDropDown
        {
            get
            {
                return new SelectList(WebcarDataHelper.BaseKind, "Item1", "Item2");
            }
        }
        public static SelectList GetChildDeptByDeptList(string deptcode)
        {
            var list = GetChildDeptByDept(deptcode);
            foreach (var item in list)
            {
                if (item.super_dept.IsNotNull())
                {
                    item.deptname = "　　" + item.deptname;
                }
            }
            return new SelectList(list, "DEPTCODE", "DEPTNAME");
        }
        public static SelectList GetCarByDeptList(string deptcode)
        {
            var list = from p in Webcar.Entitys.WebcarDataProvider.DB.BS_CAR_RECORD
                        where (p.DEPT_CODE == deptcode || p.CHILD_DEPT == deptcode)&&p.IS_USE=="T"
                        orderby p.RECORD_NO
                        select p;
            return new SelectList(list, "CAR_NO", "RECORD_NO");
        }

        public static IQueryable<v_sys_dept_all> GetChildDeptByDept(string deptcode)
        {
            var list = (from p in CurrentUser.UserGrantedDept //Webcar.Entitys.WebcarDataProvider.DB.sys_dept
                        where p.super_dept == deptcode || p.deptcode == deptcode
                        select p);
            return list;
        }
        public static SelectList CarSortDropDown
        {
            get
            {
                return new SelectList(WebcarDataHelper.CarSortCode, "Item1", "Item2");
                
            }
        }
        public static SelectList DeptKindDropDown
        {
            get
            {
                return new SelectList(WebcarDataHelper.DeptKindCode, "Item1", "Item2");

            }
        }
        public static SelectList CarUseDropDown
        {
            get
            {
                return new SelectList(WebcarDataHelper.CarUseCode, "Item1", "Item2");
            }
        }
        public static SelectList SexDropDown
        {
            get
            {
                return new SelectList(WebcarDataHelper.SexKind, "Item1", "Item2");
            }
        }
        public static SelectList CqepDeptDropDown
        {
            get
            {
                var list = WebcarDataProvider.DB.sys_cqep_dept.Where(a => a.deptcode.Length == 4).ToList();
                list.Insert(0, new sys_cqep_dept() { deptcode = "", deptname = "--请选择--" });
                return new SelectList(list, "deptcode", "deptname");
                
            }
        }
        public static List<Tuple<string, string>> AllDeptList
        {
            get
            {
                return (from p in Webcar.Entitys.WebcarDataProvider.DB.sys_dept.AsEnumerable()
                        //where p.use_flag == "T"
                        select new Tuple<string, string>(p.deptcode, p.deptname)).ToList();
            }
        }
        public static SelectList LeadCarStatus
        {
            get
            {
                List<Tuple<string, string>> list = new List<Tuple<string, string>>();
                list.Add(new Tuple<string, string>("1", "固定"));
                list.Add(new Tuple<string, string>("2", "相对固定"));
                list.Add(new Tuple<string, string>("3", "未固定"));
                
                return new SelectList(list,"Item1","Item2");
            }
        }
        public static SelectList UserSuperDeptList
        {
            get
            {
                var result = CurrentUser.UserGrantedDept.Where(a=>a.super_dept==null).ToList();
                result.Insert(0,new v_sys_dept_all() {deptcode="",deptname="--请选择--" });
                return new SelectList(result,"DEPTCODE","DEPTNAME");
            }
        }
        public static SelectList QuarterSelectList
        {
            get
            {
                List<object> list = new List<object>() { 
                    new{value="0",caption="第一季度"},
                    new{value="3",caption="第二季度"},
                    new{value="6",caption="第三季度"},
                    new{value="9",caption="第四季度"}
                };
                return new SelectList(list, "value", "caption");
            }
        }
        public static SelectList MotoSelectList
        {
            get
            {
                List<object> list = new List<object>() { 
                    new{value="all",caption="全部"},
                    new{value="not_moto",caption="不显示摩托车"},
                    new{value="moto",caption="仅摩托车"}
                };
                return new SelectList(list, "value", "caption");
            }
        }
        public static SelectList UserSuperDeptListWithAll
        {
            get
            {
                var result = CurrentUser.UserGrantedDept.Where(a => a.super_dept == null).ToList();
                result.Insert(0,new v_sys_dept_all() { deptcode = "ALL", deptname = "全部单位" });
                return new SelectList(result, "DEPTCODE", "DEPTNAME");
            }
        }
        //public static List<Tuple<string, string>> ChildDeptList
        //{
        //    get
        //    {
        //        return (from p in Webcar.Entitys.WebcarDataProvider.DB.DEPT_CHILD.AsEnumerable()
        //                select new Tuple<string, string>(p.CHILD_DEPT, p.DEPT_NAME)).ToList();
        //    }
        //}

        
    }
}